import Taro, { Component } from '@tarojs/taro'
import { View, Image, Block } from '@tarojs/components'

import './processWindow.less'

let _this

class ProcessWindow extends Component {

  static externalClasses = ['my-class']

  constructor(props) {
    super(props)
    this.state = {
      showWindow: true
    }
  }

  componentWillReceiveProps(nextProps) {
    console.log(this.props, nextProps)
  }

  componentDidMount() {
    _this = this
  }

  componentWillUnmount() { }

  componentDidShow() { }

  componentDidHide() { }

  stopPageScroll(e) {
    e.stopPropagation();
    return false
  }

  stopPropagation(e) {
    e.stopPropagation();
    return false
  }

  redirectBtnPath(e) {
    e.stopPropagation();
    if (!_this.props.inputObj.btnPath) { _this.setState({ showWindow: false }) }
    if (_this.props.inputObj.btnPath === '/pages/business/my/my') {
      if(Taro.getEnv() === Taro.ENV_TYPE.WEAPP){
        Taro.reLaunch({
          url: _this.props.inputObj.btnPath
        })
      }else{
        Taro.redirectTo({
          url: _this.props.inputObj.btnPath
        })
      }
    }
    else {
      Taro.redirectTo({ url: _this.props.inputObj.btnPath })
    }
  }

  redirectDesPath(e) {
    e.stopPropagation();
    if (!_this.props.inputObj.navigationPath) { _this.setState({ showWindow: false }) }
    if (_this.props.inputObj.navigationPath === '/pages/business/my/my') {
      if(Taro.getEnv() === Taro.ENV_TYPE.WEAPP){
        Taro.reLaunch({
          url: _this.props.inputObj.navigationPath
        })
      }else{
        Taro.redirectTo({
          url: _this.props.inputObj.navigationPath
        })
      }
    }
    else {
      Taro.redirectTo({ url: _this.props.inputObj.navigationPath })
    }
  }

  render() {
    let {inputObj} = this.props
    return (
      <View className='index'>
        {
          this.state.showWindow && <Block>
            <View className='window dis-flex flex-center' onTouchMove={this.stopPageScroll.bind(this)}>
              <View className='g vertical flex-middle' onClick={this.stopPropagation.bind(this)}>
                <Image className='g_icon' mode='aspectFill' src={inputObj.icon} />
                <View className='g_title'>{inputObj.title}</View>
                <View className='g_des'>{inputObj.des}</View>
                {inputObj.btnText && <View className='g_btn' onClick={this.redirectBtnPath.bind(this)} style={`background:${Taro.getStorageSync('baseColor')}`}>{inputObj.btnText}</View>}
                {inputObj.navigationText && <View className='g_skip' onClick={this.redirectDesPath.bind(this)}>{inputObj.navigationText}</View>}
              </View>
            </View>
          </Block>
        }

      </View>
    )
  }
}

export default ProcessWindow
